<template>
  <div></div>
</template>

<script>
import * as Three from "three";

const cabientCodePreList = ["A0", "B0", "C0", "D0", "E0", "F0", "G0", "H0"];
const personObjNameList = ["Tops", "Hair", "Body", "Bottoms", "Shoes", "Eyes"];

export default {
  name: "mouse-events",
  data() {
    return {
      selectedObjects: [],
      outlinePass: null,
      selMeshList: [],
    };
  },
  mounted() {
    // this.outlinePass = this.$refs.renderer.outlinePass;
  },
  methods: {
    // 鼠标左键单击
    mouseLeftClick(event, intersectsObjArr) {
      this.operate(event, intersectsObjArr);
    },
    // 鼠标左键双击
    mouseLeft2Click(event, intersectsObjArr) {
      this.operate(event, intersectsObjArr, 2);
    },
    operate(e, targetAry, operateType) {
      this.recoverSelMaterial();
      // this.showWidgetInfoChanged(false);
      // this.showPersonMenuChanged(false);
      if (!targetAry || targetAry.length <= 0) {
        return;
      }

      let objSelArr = targetAry.filter(obj => {
        let objName = obj.object.name;
        return (
          personObjNameList.indexOf(objName) !== -1 ||
          objName.indexOf("camra") !== -1 ||
          cabientCodePreList.indexOf(objName.substr(0, 2)) !== -1
        );
      });
      if (objSelArr.length > 0) {
        let objSel = objSelArr[0].object.parent;
        if (!objSel) {
          // 未选中物体
        } else if (objSel.name.indexOf("camera") !== -1) {
          // 选中摄像头
          this.cameraEvent(objSel, operateType);
        } else if (
          cabientCodePreList.indexOf(objSel.name.substr(0, 2)) !== -1
        ) {
          // 选中机柜
          // this.loadCabientInfo({ cabinetCode: objSel.name });
          // this.widgetInfoPosChanged({ x: e.clientX, y: e.clientY });
          this.machineEvent([objSel]);
          // this.showWidgetInfoChanged(true); // 显示小物件信息窗
          this.$emit('selObject', e, objSel, 'cabient')
        } else if (objSel.name.indexOf("person") !== -1) {
          // 选中人
          // this.currentPersonChanged({ workerCode: objSel.name.split("-")[1] });
          // this.widgetInfoPosChanged({ x: e.clientX, y: e.clientY });
          // this.showPersonMenuChanged(true);
          this.$emit('selObject', e, objSel, 'person')
        }
      } else {
        this.selectedObjects = [];
        // this.outlinePass.selectedObjects = this.selectedObjects;
      }
    },
    // operateType: 1单击操作;2双击操作
    async cameraEvent(objSel, operateType) {
      // if (operateType === 2) {
      //   // 获取摄像头实时监控
      //   this.loadCameraStream({ channelCode: objSel.name.split("_")[1] });
      // } else {
      //   // 获取摄像头覆盖的机柜
      //   let cabinets = await api.getCabinetInforByChannelId({
      //     channelId: objSel.name.split("_")[1]
      //   });
      //   let cabinetCodes = [];
      //   if (cabinets) {
      //     cabinets.forEach(cab => cabinetCodes.push(cab.cabinet_code));
      //   }
      //   this.machineEvent(
      //     this.operatedModels.filter(
      //       model => cabinetCodes.indexOf(model.name) !== -1
      //     )
      //   );
      // }
    },
    // 选中机柜时添加选中效果
    machineEvent(objSels) {
      if (objSels && objSels.length > 0) {
        this.changeSelMaterial(objSels);
        this.selectedObjects = objSels;
        this.outlinePass.selectedObjects = this.selectedObjects;
      } else {
        this.selectedObjects = [];
        this.outlinePass.selectedObjects = this.selectedObjects;
      }
    },
    // 改变选中物体的材质
    changeSelMaterial(objSels) {
      objSels.forEach(objSel => {
        for (let i = 0; i < objSel.children.length; i++) {
          let current = objSel.children[i];
          if (this.stopRepeat(current, this.selMeshList)) {
            this.selMeshList.push({
              mesh: current,
              color: current.material.color.getHex()
            });
          }
          current.material.color.set(0xff0000);
        }
      });
    },
    // 恢复选中过的物体的材质
    recoverSelMaterial() {
      this.selMeshList.forEach(selMesh => {
        selMesh.mesh.material.color.set(selMesh.color);
      });
      this.selMeshList = [];
    },
    stopRepeat(item, tarAry) {
      for (let i = 0; i < tarAry.length; i++) {
        if (item === tarAry[i].mesh) {
          return false;
        }
      }
      return true;
    }
  },
  beforeDestroy() {
    this.selectedObjects = [];
    delete this.selectedObjects;
    delete this.outlinePass;
  }
};
</script>